package com.cg.bio.case03;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * 多发和多收
 */
public class Server {
    public static void main(String[] args) throws IOException {
        //定义一个serverSocket对象进行服务端的端口注册
        ServerSocket serverSocket = new ServerSocket(9999);

        while (true){
            //监听客户端的socket连接请求
            Socket socket = serverSocket.accept();
            new ServerSocketThread(socket).start();
        }
    }
}

class ServerSocketThread extends Thread {

    private final Socket socket;

    public ServerSocketThread(Socket socket){
        this.socket = socket;
    }

    @Override
    public void run() {
        try {
            InputStream inputStream = socket.getInputStream();
            //字节输入流包装为缓冲字符输入流
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
            String msg;
            while ((msg = bufferedReader.readLine()) != null) {
                System.out.println("服务端接收到:" + msg);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
